package com.doxiaopu.students.product.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.product.entity.OrganizationSingle;
import com.doxiaopu.students.product.service.IOrganizationSingleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
@Api(tags = "组织团单信息")
@RestController
@CrossOrigin
@RequestMapping("/organization")
public class OrganizationSingleController extends BaseController {
    @Autowired
    private IOrganizationSingleService iOrganizationSingleService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //创建团单社团关系表
    @ApiOperation(value = "创建团单社团关系表")
    @PostMapping("/createOrganizationSingle")
    @ResponseBody
    public ResponseResult<Void> createOrganizationSingle(OrganizationSingle organizationSingle, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        iOrganizationSingleService.createOrganizationSingle(organizationSingle,id);
        return new ResponseResult<Void>(SUCCESS);
    }

    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        if(stringRedisTemplate.opsForValue().get(token) != null) {
            return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
        }else {
            return null;
        }
    }
}
